package org.lex.dbq.view.sql;

import java.util.Arrays;

import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.lex.dbq.model.jdbc.object.ObjectNode;
import org.lex.dbq.view.action.BaseAction;

public class BaseSqlAction extends BaseAction {
	protected final SqlPane pane;

	public BaseSqlAction(SqlPane pane) {
		this.pane = pane;
	}

	protected ObjectNode getSelectedNode() {
		ITreeSelection sel = pane.objectPane.getSelection();
		log.debug("tree selection: " + sel);
		if (sel.isEmpty()) {
			return null;
		}

		TreePath[] paths = sel.getPaths();
		log.debug("tree paths: " + Arrays.deepToString(paths));
		if (paths.length <= 0) {
			return null;
		}

		ObjectNode node = (ObjectNode) paths[0].getLastSegment();
		log.info("Selected node=" + node + " type=" + node.getType());
		return node;
	}
}
